/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen 
 * @date: Aug 5, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.plot.axis;

import edu.psu.geovista.app.pcp.toolkits.Math.JMath;

import java.awt.*;

public class BoxPlot {
    private int width=30;
    private float median;
    private float lowerQuartile;
    private float upperQuartile;
    private int labelScale=JMath.LABEL_x1 ;
    private Color color=Color.yellow;

    public BoxPlot(float[] rawdata) {
        this.setRawData(rawdata);
    }
    /**
     *
     * @param rawdata  the data on which the boxplot is made
     */
    public void setRawData(float[] rawdata) {
        float[] data=JMath.getValidData(rawdata);
        if(rawdata==null||rawdata.length <=0)return;
        this.setMedian(JMath.calculateMedian(data) );
        this.setLowerQuartile(JMath.calculateLowerQuartile(data) );
        this.setUpperQuartile(JMath.calculateUpperQuartile(data) );
    }
    /*******************************************************************************************************
     *                paint
     *******************************************************************************************************/
     public void paintComponent(Graphics g, YAxis axis) {
         if (axis.getName().startsWith("MEDIA") ){
             System.out.println("debug");
         }
         Graphics2D g2d=((Graphics2D)g);
         BasicStroke bs=new BasicStroke(2);
         g2d.setStroke(bs);
         Color old=g2d.getColor() ;
         int x=axis.getLocation().x-width/2;
         int y=axis.getLocation().y;
         //System.out.println("max:"+axis.getDataRange().getMax() +","+axis.getDataRange().getMin());
         //System.out.println("max:"+axis.getTickRange().getMax() +","+axis.getTickRange().getMin());
         int uq=(int) axis.getGraphicValueOf(this.getUpperQuartile());
         int lq=(int) axis.getGraphicValueOf(this.getLowerQuartile());
         int height=lq-uq;
         int median=(int)axis.getGraphicValueOf(this.getMedian() );
         g2d.setColor(new Color(200,200,200,150) ); // 
         g2d.fillRect(x,uq,width,height);

         g2d.setColor( getColor()); ;      //         new Color(0,180,110)
         g2d.drawRect(x,uq,width,height);
         g2d.drawString(getReadableValue(this.getMedian()) ,x-40,median);  //print median value
         g2d.setColor(Color.red );

         Stroke olds=g2d.getStroke() ;



         g2d.drawLine(x,median,x+width,median);
         g2d.setColor(old);
         (g2d).setStroke(olds);


     }
    /*******************************************************************************************************
     *                properties
     *******************************************************************************************************/
    public float getMedian() {
        return median;
    }

    public void setMedian(float median) {
        this.median = median;
    }

    public float getLowerQuartile() {
        return lowerQuartile;
    }

    public void setLowerQuartile(float lowerQuartile) {
        this.lowerQuartile = lowerQuartile;
    }

    public float getUpperQuartile() {
        return upperQuartile;
    }

    public void setUpperQuartile(float upperQuartile) {
        this.upperQuartile = upperQuartile;
    }

    public int getLabelScale() {
        return labelScale;
    }

    public void setLabelScale(int labelScale) {
        this.labelScale = labelScale;
    }

    private String  getReadableValue(float v) {
        return JMath.getScaledValue(v,this.getLabelScale() ,2);
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
}
